<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller {
	private $table_ = '' ;
	private $ROOT_ = '' ;
	function Login(){
		
		parent::__construct();
		$this->ROOT_ = str_replace("\\", "/", FCPATH);
		$this->load->model('M_upload');
		$this->load->model('M_members');
		date_default_timezone_set('Asia/Shanghai');
		$this->load->library('common');//加载admin的公用方法
		$this->table_ = $this->common->table_pre('real_data');

	
		
	}
	public function index()
	{
		@ob_clean() ;
		@session_start();
		if(isset($_SESSION['uname'])){//如果登录
				@header("Location:".site_url('users'));
		}
		$this->load->helper('captcha');
		$time_ = 60 ;
		
		$rand = substr(md5(time() . mt_rand(0,1000)), 0,3);
    	$vals = array(
			    'word' => $rand,
			    'img_path' =>$this->ROOT_.'/data/captcha/',
			    'img_url' => base_url()."data/captcha/",
			    'font_path' => BASEPATH.'/fonts/texb.ttf',
			    'img_width' => '80',
			    'img_height' => 25,
			    'expiration' => $time_,//验证码的过期时间
   		);
   
   		$cap = create_captcha($vals);
   		$data['cap'] = $cap['image'];
   		setcookie($this->config->item('cookie_prefix').'yzm',$cap['word'],time()+$time_,$this->config->item('cookie_path'),$this->config->item('cookie_domain'),$this->config->item('cookie_secure')) ;
   		
		$this->load->view('views_login',$data);//加载登录
	}
	
	public function do_login(){
		$username = strip_tags(html_escape(daddslashes($this->input->get_post("uname",true)))) ; 
		$passwd = strip_tags(html_escape(daddslashes($this->input->get_post("passwd",true)))) ; 
		$yzm = strip_tags(html_escape(daddslashes($this->input->get_post("yzm",true)))) ; 
		$username = isset($username)?$username:'' ;
		$passwd = isset($passwd)?$passwd:'' ;
		$yzm = isset($yzm)?$yzm:'' ;
		
		if($username == "" || $passwd == "" ){
			showmessage("用户名或者密码不可以为空",'login/index',3,0);
			die();
		}
		if($yzm != $_COOKIE[$this->config->item('cookie_prefix')."yzm"]){
			showmessage("验证码错误",'login/index',3,0);
			die();
		}
		$passwd = md5($passwd);
		$sql_ = "SELECT `username` FROM {$this->table_}common_member where username = '{$username}' AND passwd = '{$passwd}' and `status` = 1  limit 1 " ;
	
		$row = $this->M_members->query_one($sql_);
		if(empty($row)){
			showmessage("用户名或者密码错误,或者已经被禁用",'login/index',3,0);
			die();
		}
		@ob_start();
		@ob_clean();
		@session_start();
		$_SESSION['uname'] = $username ;
		showmessage("登录成功","users/index",3,1);
	}
	
	public function login_out(){
		@ob_clean() ;
		@session_start();
		if(isset($_SESSION['uname']) && !empty($_SESSION['uname'])){
			$_SESSION['uname'] = '' ;
			unset($_SESSION['uname']);
			$_SESSION['client_ip'] = '' ;
			unset($_SESSION['client_ip']);
			showmessage('退出成功','login',3,1) ;
		}
	
	}



}

